package com.asialjim.wechat.wechataccount.impl;

import com.asialjim.wechat.database.DataBaseService;
import com.asialjim.wechat.pojo.account.WeChatAccount;
import com.asialjim.wechat.pojo.account.WeChatAccountHolder;
import com.asialjim.wechat.wechataccount.WeChatAccountService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: WeChatAccountServiceImpl ]</i></h2><strong>
 * <p> we-chat account service implements
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/11   &nbsp;&nbsp; JDK 8
 */
@Data
@Slf4j
@AllArgsConstructor
public class WeChatAccountServiceImpl implements WeChatAccountService {
    private DataBaseService dataBaseService;

    @Override
    public List<WeChatAccount> allAccounts() {
        return dataBaseService.selectList(WeChatAccount.builder().build());
    }

    @Override
    public WeChatAccount account(String accountId) {
        return dataBaseService.selectById(WeChatAccount.class, accountId);
    }

    @Override
    public void deCacheAllAccount() {
        // implement by annotation
    }

    @Override
    public WeChatAccount findBySubjectId(String subjectId) {
        return dataBaseService.selectOne(WeChatAccount.builder().subjectId(subjectId).build());
    }

    @Override
    public boolean update(WeChatAccount account) {
        int i = dataBaseService.updateById(account);
        if (i == 1) {
            deCacheAllAccount();
        }
        return i == 1;
    }

    @Override
    public boolean add(WeChatAccount account) {
        int insert = dataBaseService.insert(account);
        if (insert == 1) {
            deCacheAllAccount();
        }
        return insert == 1;
    }

    @Override
    public boolean delete(String accountId) {
        int i = dataBaseService.deleteById(WeChatAccount.class, accountId);
        if (i == 1) {
            deCacheAllAccount();
        }
        return i == 1;
    }

    @Override
    public String currentAppId() {
        String appId = "";
        WeChatAccount weChatAccount = WeChatAccountHolder.get();
        String toUserName = WeChatAccountHolder.getToUserName();
        if (Objects.nonNull(weChatAccount) && StringUtils.isNotBlank(weChatAccount.getAppId())) {
            appId = weChatAccount.getAppId();
        } else if (StringUtils.isNotBlank(toUserName)) {
            WeChatAccount condition = WeChatAccount.builder().subjectId(toUserName).build();
            weChatAccount = dataBaseService.selectOne(condition);
            if (Objects.nonNull(weChatAccount)) {
                WeChatAccountHolder.set(weChatAccount);
                appId = weChatAccount.getAppId();
            }
        }

        return appId;
    }
}